package Form;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.DefaultComboBoxModel;

import Class.Employee;
import Class.Procedure;
import Support.International;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class ForgotPassword {

	//security
	private JTextField tfForgotLoginName;
	private JTextField tfForgotAnswer;
	private JComboBox cbForgotQuestion;
	
	//use for multi-language
	public static JFrame frmForgotPassword;
	public static JLabel lbForgotLoginName;
	public static JLabel lbForgotQuestion;
	public static JLabel lbForgotAnswer;
	public static JButton btnForgotSend;
	public static JButton btnForgotCancel;
	public static JLabel lbForgotNoteEmail;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
//				try {
//					ForgotPassword window = new ForgotPassword();
//					window.frmForgotPassword.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
			}
		});
	}

	/**
	 * Create the application.
	 * @wbp.parser.entryPoint
	 */
	public ForgotPassword() {
		initialize();
		if(Login.flgLanguage == 0)
		{
			International.locale = new Locale("en","UK");
			International.setLanguageForgotPassword(International.locale);
		}
		else 
		{
			International.locale = new Locale("vi","VN");
			International.setLanguageForgotPassword(International.locale);
		}
		this.frmForgotPassword.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.frmForgotPassword.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frmForgotPassword = new JFrame();
		frmForgotPassword.setTitle("Forgot password");
		frmForgotPassword.setBounds(100, 100, 451, 237);
		frmForgotPassword.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmForgotPassword.getContentPane().setLayout(null);
		
		lbForgotLoginName = new JLabel("Login name");
		lbForgotLoginName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbForgotLoginName.setBounds(36, 38, 116, 21);
		frmForgotPassword.getContentPane().add(lbForgotLoginName);
		
		lbForgotQuestion = new JLabel("Secret question");
		lbForgotQuestion.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbForgotQuestion.setBounds(36, 70, 105, 21);
		frmForgotPassword.getContentPane().add(lbForgotQuestion);
		
		lbForgotAnswer = new JLabel("Answer");
		lbForgotAnswer.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbForgotAnswer.setBounds(36, 108, 74, 21);
		frmForgotPassword.getContentPane().add(lbForgotAnswer);
		
		tfForgotLoginName = new JTextField();
		tfForgotLoginName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if(evt.getKeyCode() == KeyEvent.VK_ENTER)
				{
					if(!tfForgotLoginName.getText().equals("") && !tfForgotAnswer.getText().equals(""))
					{
						String loginName = tfForgotLoginName.getText();
						Employee emp = Procedure.getMyInfo(loginName);
						Procedure.forgotPassword(loginName,cbForgotQuestion.getSelectedIndex(),tfForgotAnswer.getText(),emp.getMail());
					}
					else
					{
						JOptionPane.showMessageDialog(null, "All blanks must be filled in!!!","Warning",JOptionPane.WARNING_MESSAGE);
					}
				}
			}
		});
		tfForgotLoginName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tfForgotLoginName.setBounds(162, 35, 126, 26);
		frmForgotPassword.getContentPane().add(tfForgotLoginName);
		tfForgotLoginName.setColumns(10);
		
		tfForgotAnswer = new JTextField();
		tfForgotAnswer.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if(evt.getKeyCode() == KeyEvent.VK_ENTER)
				{
					if(!tfForgotLoginName.getText().equals("") && !tfForgotAnswer.getText().equals(""))
					{
						String loginName = tfForgotLoginName.getText();
						Employee emp = Procedure.getMyInfo(loginName);
						Procedure.forgotPassword(loginName,cbForgotQuestion.getSelectedIndex(),tfForgotAnswer.getText(),emp.getMail());
					}
					else
					{
						JOptionPane.showMessageDialog(null, "All blanks must be filled in!!!","Warning",JOptionPane.WARNING_MESSAGE);
					}
				}
			}
		});
		tfForgotAnswer.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tfForgotAnswer.setColumns(10);
		tfForgotAnswer.setBounds(162, 105, 153, 26);
		frmForgotPassword.getContentPane().add(tfForgotAnswer);
		
		cbForgotQuestion = new JComboBox();
		cbForgotQuestion.setFont(new Font("Tahoma", Font.PLAIN, 13));
		cbForgotQuestion.setModel(new DefaultComboBoxModel(new String[] {"What is your favourite cartoon ?", "Where do you want to travel to ?", "When did you have your first kiss ?", "Who do you love the most ?", "What is the name of your primary school ?"}));
		cbForgotQuestion.setBounds(162, 68, 271, 26);
		frmForgotPassword.getContentPane().add(cbForgotQuestion);
		
		btnForgotSend = new JButton("Send");
		btnForgotSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if(!tfForgotLoginName.getText().equals("") && !tfForgotAnswer.getText().equals(""))
				{
					String loginName = tfForgotLoginName.getText();
					Employee emp = Procedure.getMyInfo(loginName);
					Procedure.forgotPassword(loginName,cbForgotQuestion.getSelectedIndex(),tfForgotAnswer.getText(),emp.getMail());
				}
				else
				{
					JOptionPane.showMessageDialog(null, "All blanks must be filled in!!!","Warning",JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		btnForgotSend.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnForgotSend.setBounds(61, 173, 91, 26);
		frmForgotPassword.getContentPane().add(btnForgotSend);
		
		btnForgotCancel = new JButton("Cancel");
		btnForgotCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				frmForgotPassword.setVisible(false);
				frmForgotPassword.dispose();
			}
		});
		btnForgotCancel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnForgotCancel.setBounds(255, 172, 91, 27);
		frmForgotPassword.getContentPane().add(btnForgotCancel);
		
		lbForgotNoteEmail = new JLabel("Note : Your email will receive new password");
		lbForgotNoteEmail.setForeground(Color.RED);
		lbForgotNoteEmail.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbForgotNoteEmail.setBounds(36, 140, 332, 21);
		frmForgotPassword.getContentPane().add(lbForgotNoteEmail);
	}
}
